<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace modules\sys\service\impl;

use application\core\Result;
use application\core\SnowFlake;
use modules\sys\component\SetCacheComponent;
use modules\sys\mapper\ActionMapper;
use modules\sys\mapper\PageMapper;
use modules\sys\mapper\RolePageActionMapper;
use modules\sys\service\PageService;

class PageServiceImpl implements PageService
{


    public function lists(array $data, array $pagePars): array
    {
        $lists = PageMapper::lists($data, $pagePars);
        $rows = $lists['rows'] ?? $lists;
        $listsRole = [];
        if(!empty($data['role_id'])){
            //查询该角色页面权限
            $listsRole = RolePageActionMapper::listsByRoleIdPageId(['role_id' => $data['role_id']]);
            $listsRole = array_column($listsRole, NULL, 'page_id');
        }
        foreach ($rows as $k => $v) {
            $pageActionIdsArr = [];
            if(!empty($data['role_id'])) {
                $pageActionIdsArr = explode(",",$listsRole[$v['id']]['action_ids']);
            }
            //先初始化操作功能数组
            $actionArr = [];
            $actionArr[] = [
                'id' => $v['id'] . '@auth',
                'action_name' => '接口授权',
                'code' => $v['code'],
                'api_url' => $v['api_url'],
                'checked' => in_array("auth",$pageActionIdsArr),
                'sort' => 0,
                'status' => 1
            ];
            $actionRs = ActionMapper::listsByPageId(['page_id' => $v['id']]);
            foreach ($actionRs as $_actionV) {
                $_actionV['checked'] = in_array($_actionV['id'],$pageActionIdsArr);
                $_actionV['id'] = $v['id']."@".$_actionV['id'];
                $actionArr[] = $_actionV;
            }
            if ($lists['rows']) {
                if(!empty($data['role_id'])) {
                    $lists['rows'][$k]['checked'] = isset($listsRole[$v['id']]);
                }
                $lists['rows'][$k]['action'] = $actionArr;
            } else {
                if(!empty($data['role_id'])) {
                    $lists[$k]['checked'] = isset($listsRole[$v['id']]);
                }
                $lists[$k]['action'] = $actionArr;
            }
        }
        return Result::success(false,$lists);
    }

    public function add(array $data): array
    {
        $data['id'] = SnowFlake::instance()->nextId();
        $add = PageMapper::add($data);
        if ($add == 0) {
            return Result::fail(false,[], "添加失败");
        }
        SetCacheComponent::page();
        return Result::success(true,[], "添加成功");
    }

    public function edit(array $data): array
    {
        $edit = PageMapper::edit($data);
        if ($edit == 0) {
            return Result::fail(false,[], "暂未修改任何数据");
        }
        SetCacheComponent::page();
        return Result::success(true,[], "修改成功");
    }

    public function query(array $data): array
    {
        return Result::success(false,PageMapper::query($data));
    }

    function del(array $data): array
    {

        //删除页面先查询是否还有操作功能，如果有操作功能提示先删除操作功能
        $listsByPageIdRs = ActionMapper::listsByPageId(['page_id' => $data['ids']]);
        if (!empty($listsByPageIdRs)) {
            return Result::fail(false,[], "该页面下还存在操作功能，请先删除该页面的操作功能");
        }
        $del = PageMapper::del(['id' => $data['ids']]);
        if ($del == 0) {
            return Result::fail(false,[], "删除失败");
        }
        SetCacheComponent::page();
        return Result::success(true,[], "删除成功");
    }

    function initBtn(array $data): array
    {
        $snowFlake = SnowFlake::instance();
        $pageId = $data['page_id'];
        $actions = $data['actions'];
        $actionsArr = explode(",", $actions);
        $pageRs = PageMapper::query(['id' => $pageId]);
        $tableName = $pageRs['table_name'];
        $pageName = $pageRs['page_name'];
        $pageCode = $pageRs['code'];
        $pageCodeArr = explode(':', $pageCode);
        $codePre = $pageCodeArr[0] . ":" . $pageCodeArr[1] . ":";
        foreach ($actionsArr as $action) {
            $mapData = [
                'id' => $snowFlake->nextId(),
                'page_id' => $pageId,
                'action_name' => '',
                'table_name' => $tableName,
                'code' => $codePre . $action,
                'bind_event' => $action,
                'tips' => '#',
                'bg_color' => '',
                'target_type' => '',
                'width' => '100%',
                'height' => '100%',
                'jump' => '#',
                'query_url' => '#',
                'api_url' => '#',
                'status' => 1,
                'sort' => 255
            ];
            if ($action == 'add') {
                $mapData['action_name'] = "添加";
                $mapData['tips'] = "添加" . $pageName;
                $mapData['bg_color'] = "#009688";
                $mapData['target_type'] = "openFrame";
                $mapData['jump'] = "add.html";
                $mapData['api_url'] = str_replace(":", "/", $codePre . $action);
            } elseif ($action == 'del') {
                $mapData['action_name'] = "删除";
                $mapData['tips'] = "确定要删除选中的数据吗？";
                $mapData['bg_color'] = "#ff5722";
                $mapData['target_type'] = "doApi";
                $mapData['api_url'] = str_replace(":", "/", $codePre . $action);
            } elseif ($action == 'delAll') {
                $mapData['action_name'] = "批量删除";
                $mapData['tips'] = "确定要删除选中的数据吗？";
                $mapData['bg_color'] = "#ff0000";
                $mapData['target_type'] = "doApi";
                $mapData['api_url'] = str_replace(":", "/", $codePre . $action);
            } elseif ($action == 'edit') {
                $mapData['action_name'] = "编辑";
                $mapData['tips'] = "编辑" . $pageName;
                $mapData['bg_color'] = "#1e9fff";
                $mapData['target_type'] = "openFrame";
                $mapData['jump'] = "edit.html";
                $mapData['api_url'] = str_replace(":", "/", $codePre . $action);
                $mapData['query_url'] = str_replace(":", "/", $codePre . "query");
            }
            $queryByPageIdActionCodeRs = ActionMapper::queryByPageIdActionCode(['page_id' => $pageId, 'code' => $codePre . $action]);
            if (empty($queryByPageIdActionCodeRs)) {
                ActionMapper::add($mapData);
            }
        }
        SetCacheComponent::page();
        return Result::success(true,[], '初始化成功');
    }
}

